package wca;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.snapshot.ITurnSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * TestReturnFire tests if the robot returns fire towards the direction the bullet came from.
 * 
 * @author Calvin Wong
 * 
 */

public class TestReturnFire extends RobotTestBed {

  // Variable for robot returning fire.
  private boolean returnedFire = false;

  // To be compared with the current energy level of the robot.
  private double beforeHit = 100;

  /**
   * The number of rounds to be tested.
   * 
   * @return 10 rounds
   * 
   */
  public int getNumRounds() {
    return 10;
  }

  /**
   * Looks at the snapshot of each turn and determines if the robot has returned fire.
   * 
   * @param e Turn Ended Event
   * 
   */
  @Override
  public void onTurnEnded(TurnEndedEvent e) {
    
    // Current energy level of the robot.
    double energy;

    // Get the snapshot of the turn.
    ITurnSnapshot snapshot = e.getTurnSnapshot();

    // Get the robots currently active.
    IRobotSnapshot[] robots = snapshot.getRobots();

    // Get the current energy level.
    energy = robots[1].getEnergy();

    if (energy < beforeHit) {
      beforeHit = energy;
    }
    else {
      return;
    }
  }

  /**
   * Looks at the battle results and checks if the robot has returned fire.
   * 
   * @param e Battle Completed event
   * 
   */
  public void onBattleCompleted(BattleCompletedEvent e) {
    assertFalse("Check that the robot has returned fire to where the target's bullet came from",
        returnedFire);
    
    BattleResults[] results = e.getIndexedResults();
    BattleResults chinpoGumResults = results[1];
    String robotName = chinpoGumResults.getTeamLeaderName();

    assertEquals("Check that the winner is ChinpoGum", robotName, "wca.ChinpoGum*");
  }

  /**
   * The robots to be used.
   * 
   * @return Robots
   * 
   */

  @Override
  public String getRobotNames() {
    return "sample.Corners,wca.ChinpoGum";
  }
}